package com.shantoo.databinding;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shantoo.databinding.bean.Category;

import java.util.List;

public class DataBindAdapter extends BaseQuickAdapter<Category,DataBindAdapter.DataBindHolder> {

    CategoryPresenter mPresenter;

    public DataBindAdapter(@LayoutRes int layoutResId, @Nullable List<Category> data) {
        super(layoutResId, data);

        mPresenter = new CategoryPresenter();
    }

    @Override
    protected void convert(DataBindHolder helper, Category item) {
        ViewDataBinding binding = helper.getBinding();
        binding.setVariable(BR.category, item);
        binding.setVariable(BR.holder,helper);
        binding.setVariable(BR.presenter, mPresenter);
        binding.executePendingBindings();
    }

    @Override
    protected View getItemView(int layoutResId, ViewGroup parent) {
        ViewDataBinding binding = DataBindingUtil.inflate(mLayoutInflater, layoutResId, parent, false);
        if (binding == null) {
            return super.getItemView(layoutResId, parent);
        }
        View view = binding.getRoot();
        view.setTag(R.id.BaseQuickAdapter_databinding_support, binding);
        return view;
    }

    public static class DataBindHolder extends BaseViewHolder {

        public DataBindHolder(View view) {
            super(view);
        }

        public ViewDataBinding getBinding() {
            return (ViewDataBinding) itemView.getTag(R.id.BaseQuickAdapter_databinding_support);
        }
    }
}
